package org.example.zuoye.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.zuoye.entity.Admin;
import org.example.zuoye.mapper.AdminMapper;
import org.example.zuoye.service.AdminService;
import org.jasypt.util.password.PasswordEncryptor;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/08/10/ 16:06
 * @Description
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 分页查询
     */
    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Admin admin) {
        try(Page<?> __ = PageHelper.startPage(page, limit)) {
            List list = adminMapper.search(admin);
            PageInfo pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }

    /**
     * 新增操作
     */
    @Override
    public int add(Admin admin) throws Exception {
        Admin sAdmin = adminMapper.selectByPhone(admin.getPhone());
        if (!ObjectUtils.isEmpty(sAdmin)) {
            throw new RuntimeException("该手机号已存在");
        }
        if (StringUtils.hasText(admin.getUsername())) {
            Admin cAdmin = adminMapper.selectByUserName(admin.getUsername());
            if (!ObjectUtils.isEmpty(cAdmin)) {
                throw new Exception("用户名已存在！");
            }
        }

        PasswordEncryptor pe  = new StrongPasswordEncryptor();
        String pass = pe.encryptPassword(admin.getPassword());
        admin.setPassword(pass);
        return adminMapper.insertSelective(admin);

    }

    @Override
    public int remove(Integer[] ids) throws Exception {
        return adminMapper.deleteByIds(ids);
    }

    @Override
    public int edit(Admin admin) throws Exception {
        if (StringUtils.hasText(admin.getPhone())) {
            Admin sAdmin = adminMapper.selectByPhone(admin.getPhone());
            if (!ObjectUtils.isEmpty(sAdmin) && !admin.getId().equals(sAdmin.getId())) {
                throw new Exception("手机号已存在！");
            }
        }
        if (!ObjectUtils.isEmpty(admin.getUsername())) {
            Admin sAdmin = adminMapper.selectByUserName(admin.getUsername());
            if (!ObjectUtils.isEmpty(sAdmin) && !admin.getId().equals(sAdmin.getId())) {
                throw new Exception("用户名已存在！");
            }
        }

        if (!ObjectUtils.isEmpty(admin.getPassword())) {
            PasswordEncryptor pe  = new StrongPasswordEncryptor();
            String pass = pe.encryptPassword(admin.getPassword());
            admin.setPassword(pass);
        }
        return adminMapper.update(admin);

    }

    /**
     * 根据用户名查询
     * @param username 用户名
     * @return
     */
    @Override
    public Admin findByUsername(String username) {
        return adminMapper.selectByUserName(username);
    }


}
